var normal = document.getElementById('mode-normal');
var pressure = document.getElementById('mode-pressure');
$(function () {
    $('.mode-checkbox').on('click', function () {//设置模式checkbox 互斥
        mode_set_checkbox(this);
    });
    getDeviceList(1);
    $('.weui-navbar__item').on('click', function () {
        var url = $(this).attr('to-url');
        $(this).addClass('weui-bar__item_on').siblings('.weui-bar__item_on').removeClass('weui-bar__item_on');
        $(jQuery(this).attr("href")).show().siblings('.weui-tab__content').hide();
        if (url === 'getDeviceList') {
            // getDeviceList(1);
        } else if (url === 'testerMode') {
            getTesterMode(1, 1, 1);
        }
    });

    // Example functionality to demonstrate a value feedback
    // and change the output's value.
    function valueOutput(element, flag) {
        var value = element.value;
        var output = element.parentNode.getElementsByTagName('output')[0];
        var setValue = element.parentNode.getElementsByClassName('value')[0];
        if (flag == 1) {
            output.innerHTML = (value / 60) + "小时";
            setValue.value = (value / 60);
        } else if (flag == 2) {
            output.innerHTML = value + "分钟";
            setValue.value = value;
        } else if (flag == 3) {
            output.innerHTML = value + "%";
            setValue.value = value;
        }

    }

    // Initial value output
    var selector = '[data-rangeslider]';
    var $inputRange = $(selector);
    for (var i = $inputRange.length - 1; i >= 0; i--) {
        valueOutput($inputRange[i]);
    }
    // Update value output
    $('#test-period-slider').on('input', function (e) {
        valueOutput(e.target, 1);
    });
    $('#cmd-interval-slider').on('input', function (e) {
        valueOutput(e.target, 2);
    });
    $('#success-radio-div').on('input', function (e) {
        valueOutput(e.target, 3);
    });

    // Initialize the elements
    $inputRange.rangeslider({
        polyfill: false
    });
});

function mode_set_checkbox(bean) {// //设置模式checkbox 互斥
    if (bean == normal) {
        if (normal.checked) {
            document.getElementById('test-mode-slider-div').style.display = 'none';
            pressure.checked = false;
        }
        else {
            document.getElementById('test-mode-slider-div').style.display = 'block';
            pressure.checked = true;
        }
    } else {
        if (pressure.checked) {
            document.getElementById('test-mode-slider-div').style.display = 'block';
            normal.checked = false;
        } else {
            document.getElementById('test-mode-slider-div').style.display = 'none';
            normal.checked = true;
        }

    }
}

function getTesterMode(testerId, tenantId, productId) {//测试模式查询
    $.ajax({
        type: "get",
        url: common.getTesterMode,
        data: {"testerId": testerId, "tenantId": tenantId, "productId": productId},
        success: function (result) {
            //这里是解析的json数据   直接返回data
            var resultData = result;
            var data = resultData.data;
            if (resultData.code == 0) {
                if (data.testType == 1) {
                    document.getElementById('test-mode-slider-div').style.display = 'block';
                    pressure.checked = true;
                    normal.checked = false;
                    $('#test-period-slider').val(data.testPeriod * 60).change();
                    $('#cmd-interval-slider').val(data.cmdInterval).change();
                    $('#success-radio-slider').val(data.successRadio).change();
                    $('#cmdInterval').val(data.testPeriod);
                    $('#testPeriod').val(data.cmdInterval);
                    $('#successRadio').val(data.successRadio);
                } else {
                    document.getElementById('test-mode-slider-div').style.display = 'none';
                    pressure.checked = false;
                    normal.checked = true;
                }
            } else {
                common.showMsgTips(resultData.msg);
            }
            //数据格式
            //[{"code":0,"data":[{"attributeName":"温度","attributeValue":"0"},{"attributeName":"温度","attributeValue":"0"}],"msg":""}]
        }
    });
}

function submitTesterMode() {//测试模式查询
    var testerMode = {};
    testerMode['tenantId'] = $('#tenantId').val();
    testerMode['testerId'] = $('#testerId').val();
    testerMode['productId'] = $('#productId').val();
    testerMode['testType'] = $('#testType').val();
    testerMode['testPeriod'] = $('#testPeriod').val();
    testerMode['cmdInterval'] = $('#cmdInterval').val();
    testerMode['successRadio'] = $('#successRadio').val();
    if (normal.checked) {//正常选择
        $('#testType').val(0);
    } else {//正常未选择
        $('#testType').val(1);
    }
    $.ajax({
        type: "get",
        url: common.submitTesterMode,
        data: testerMode,
        success: function (result) {
            //这里是解析的json数据   直接返回data
            var resultData = result;
            console.log(resultData);
            var data = resultData.data;
            if (resultData.code == 0) {
                common.showMsgTips("操作成功");
            } else {
                common.showMsgTips(resultData.msg);
            }
            //数据格式
            //[{"code":0,"data":[{"attributeName":"温度","attributeValue":"0"},{"attributeName":"温度","attributeValue":"0"}],"msg":""}]
        }
    });
}

function getDeviceList(deviceTypeId) {//设备模型属性查询
    $.ajax({
        type: "get",
        url: common.getDeviceList,
        data: {"deviceTypeId": deviceTypeId},
        success: function (result) {
            //这里是解析的json数据   直接返回data
            assembleDeviceRule(result);
            //数据格式
            //[{"code":0,"data":[{"attributeName":"温度","attributeValue":"0"},{"attributeName":"温度","attributeValue":"0"}],"msg":""}]
        }
    });
}

function checkDeviceRule(deviceTypeId, testerId) {//校验规则查询
    $.ajax({
        type: "get",
        url: common.checkDeviceRule,
        data: {"deviceTypeId": deviceTypeId, "testerId": testerId},
        success: function (result) {
            //这里是解析的json数据   直接返回data
            var resultData = result;
            var list = resultData.data;
            if (resultData.code == 0) {
                for (var i = 0; i < list.length; i++) {
                    var _o = list[i];
                    console.log(_o.datailId);
                    $('#setValue' + _o.datailId).val(_o.setValue);
                    $('#max' + _o.datailId).val(_o.max);
                    $('#min' + _o.datailId).val(_o.min);
                }
            } else {
                common.showMsgTips(resultData.msg);
            }
            //数据格式
            //[{"code":0,"data":[{"attributeName":"温度","attributeValue":"0"},{"attributeName":"温度","attributeValue":"0"}],"msg":""}]
        }
    });
}

function assembleDeviceRule(resultData) {
    var list = resultData.data;
    if (resultData.code == 0) {
        var str = '';//device
        for (var i = 0; i < list.length; i++) {
            var _o = list[i];
            str += '<div class="weui-flex sg-test-body"><div class="weui-flex__item">';
            str += '<input type="hidden" name="deviceRecordList[' + i + '].detailId" value="' + list[i].datailId + '">';
            str += '<div class="placeholder">' + _o.detailName + '</div>';
            str += '</div><div class="weui-flex__item"><div class="placeholder">';
            if (_o.dataType > 1) str += '<div style="font-size: 0.8rem;">';
            else str += '<div class="placeholder-radio-div">';
            str += '<input type="radio" value="1" name="deviceRecordList[' + i + '].isNull" ';
            if (_o.editRules == 0) str += ' disabled ';
            if (_o.isNull == 1) str += 'checked';
            str += '>可为空&nbsp;&nbsp;';
            str += '<input type="radio" value="0" name="deviceRecordList[' + i + '].isNull" ';
            if (_o.editRules == 0) str += ' disabled ';
            if (_o.isNull == 0) str += 'checked';
            str += '>不可为空';
            if (_o.dataType <= 1) str += '<div class="placeholder-min-max-div">';

            str += '<input type="';
            if (_o.dataType <= 1) str += 'text'; else str += 'hidden';
            str += '" placeholder="最小值" name="deviceRecordList[' + i + '].min" id="min' + list[i].datailId + '" ';
            if (_o.editRules == 0) str += ' disabled ';
            str += '> - <input type="';
            if (_o.dataType <= 1) str += 'text'; else str += 'hidden';
            str += '" placeholder="最大值" name="deviceRecordList[' + i + '].max" id="max' + list[i].datailId + '" ';
            if (_o.editRules == 0) str += ' disabled ';
            str += '>';
            if (_o.dataType <= 1) str += '</div>';
            str += '</div></div></div>';
            str += '<div class="weui-flex__item"><div class="placeholder placeholder-value-div">';
            str += '<input type="text" placeholder="请输入数值"  name="deviceRecordList[' + i + '].setValue" id="setValue' + list[i].datailId + '" ';
            if (_o.editRules == 0) str += ' disabled ';
            str += '></div></div></div>';
        }
        $('#test-rule-page').html(str);

    } else {
        common.showMsgTips(resultData.msg);
    }
    checkDeviceRule(1, 1);
}

function saveDeviceRule() {
    var testerMode = {};
    testerMode['tenantId'] = $('#tenantId').val();
    testerMode['testerId'] = $('#testerId').val();
    testerMode['productId'] = $('#productId').val();
    testerMode['deviceTypeId'] = $('#deviceTypeId').val();
    $('#test-rule-page').find(':input').each(function () {
        if ($(this).attr('type') === 'radio') {
            if (this.checked) testerMode[$(this).attr('name').toString()] = $(this).val();
        } else {
            testerMode[$(this).attr('name').toString()] = $(this).val();
        }
    });
    $.ajax({
        type: "get",
        url: common.saveDeviceRule,
        data: testerMode,
        success: function (result) {
            //这里是解析的json数据   直接返回data
            var resultData = result;
            var list = resultData.data;
            if (resultData.code == 0) {
                common.showMsgTips("保存成功");
            } else {
                common.showMsgTips(resultData.msg);
            }
            //数据格式
            //[{"code":0,"data":[{"attributeName":"温度","attributeValue":"0"},{"attributeName":"温度","attributeValue":"0"}],"msg":""}]
        }
    });
}
